from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from myupup.models import MyupUser,MyupCoupon,MyupGoods,MyupIndex
from myupup.myserializers import MyupUserSer,MyupCoodsSer,MyupIndexSer,MyupCouponSer
# Create your views here.
class MyupdeUser(APIView):
    def get(self,request):
        myuser=MyupUser.objects.all()
        myuserser=MyupUserSer(myuser,many=True)
        return Response({'message':myuserser.data})
class MyupdeLogin(APIView):
    def get(self,request):
        username=request.GET.get('username')
        password=request.GET.get('password')
        users=MyupUser.objects.filter(username=username,password=password).first()
        if users:
            return Response({'code':200,'message':"登录成功",'username':users.username,'uid':users.id})

class MyupdeUp(APIView):
    def get(self,request):
        username=request.GET.get('username')
        user = MyupUser.objects.get(username=username)
        if user:
            user.vipuser = 2
            user.userprice=2
            user.save()

        return Response({'message': '升级成功'})

class MyupdeGoods(APIView):
    def get(self,request):
        myuser=MyupGoods.objects.all()
        myuserser=MyupCoodsSer(myuser,many=True)
        return Response({'message':myuserser.data})

class MyupdeCart(APIView):
    def post(self,request):

        num=request.data['num']
        title=request.data['title']
        price=request.data['price']
        good=MyupGoods.objects.filter(title=title)
        if good:
            good.num+=int(num)
            good.save()
            if good.num>10:
                return Response({'mesage':'超过库存'})

        return Response({"message":'提交成功'})




        data = request.data
        ser = MyupIndexSer(data=data)
        if ser.is_valid():
            ser.save()
        return Response({'订单提交成功'})


class MyupdeCartlist(APIView):
    def get(self,request):
        myuser=MyupIndex.objects.all()
        myuserser=MyupIndexSer(myuser,many=True)
        return Response({'message':myuserser.data})

class MyupdeCoupon(APIView):
    def get(self,request):
        mycoupon = MyupCoupon.objects.all()
        mycouponser = MyupCouponSer(mycoupon, many=True)
        return Response({'message': mycouponser.data})